package main.controller;


import javax.servlet.http.HttpServletRequest;

import main.entity.UserVO;
import main.service.BaseService;
import main.service.IBaseService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.servlet.ModelAndView;

//@Controller
public class BaseCtrl<T> {

	@Autowired
	@Qualifier("baseService")
	private IBaseService<T> baseService;
//	private BaseService<T> baseService = new BaseService<T>();
	
	protected String targetURL;
	protected ModelAndView mdv;
	
	protected BaseCtrl() {
		super();
		this.targetURL = new String("");
		this.mdv = new ModelAndView();
	}

	public String getTargetURL() {
		return targetURL;
	}

	public void setTargetURL(String targetURL) {
		this.targetURL = targetURL;
	}

	public IBaseService<T> getIBaseService() {
		return this.baseService;
	}

/*	public void setThreadsDAO(IBaseDAO<ThreadsVO> threadsDAO) {
		this.threadsDAO = threadsDAO;
	}*/


	/**
	 * 所有Controller的基类
	 * </pre>
	 * @see
	 */
	    protected static final String ERROR_MSG_KEY = "errorMsg";

	    /**
	     * 获取保存在Session中的用户对象
	     *
	     * @param request
	     * @return
	     */
	    protected UserVO getSessionUser(HttpServletRequest request, String username) {
//	        return (User) request.getSession().getAttribute(
//	                CommonConstant.USER_CONTEXT);
	    	return (UserVO) request.getSession().getAttribute(username);
	    }

	    /**
	     * 保存用户对象到Session中
	     * @param request
	     * @param user
	     */
	    protected void setSessionUser(HttpServletRequest request, UserVO userVO) {
	        /*request.getSession().setAttribute(CommonConstant.USER_CONTEXT,
	                user);*/
	    	request.getSession().setAttribute(userVO.getUsername(), userVO);
	    }


	    /**
	     * 获取基于应用程序的url绝对路径
	     *
	     * @param request
	     * @param url
	     *            以"/"打头的URL地址
	     * @return 基于应用程序的url绝对路径
	     */
	    public final String getAppbaseUrl(HttpServletRequest request, String url) {
	        Assert.hasLength(url, "url不能为空");
	        Assert.isTrue(url.startsWith("/"), "必须以/打头");
	        return request.getContextPath() + url;
	    }


}
